package core.weapons;
import java.awt.*;
//import java.util.ArrayList;

import core.Player;
import core.SingleButtonGame;
import core.units.Unit;

public class Attack {
	Player master;
	SingleButtonGame main;
	
	boolean hasHit=false;	
	protected int speed = 1, lifetime = 300;
	protected int team, player;
	protected int x, y;
	protected Point currentPosition, lastPosition;
	protected double dir;
	protected Rectangle bounds;
	protected final int FACTOR=1000;
	protected int damage;
	//ArrayList<String> attackQueue = new ArrayList<String>();
	
	public Attack(int x1, int y1, Player m){
		main=SingleButtonGame.getInstance();
		master=m;
		bounds=new Rectangle(x-5, y-5, 10, 10);
		team=m.getTeam();
		player=m.getPlayer();
		x=x1;
		y=y1;
		currentPosition = new Point(x, y);
		lastPosition = (Point) currentPosition.clone();
		damage=100;
		//m.numberOfUnits++;
	}
	public Attack(Point p, Player m){
		this(p.x, p.y, m);
	}
	
	public void checkHit(Unit u){
		if(u.getTeam()!=team && bounds.intersects(u.bounds)){
			u.takeDamage(100);
			//System.out.print("i");
		}
			
	}
	
	public void nextFrame(){
		lifetime--;
		if(lifetime==0)
			main.attacks.remove(this);
	}
	public void paint(Graphics g){
		g.setColor(Color.blue);
		g.fillRect(x-5, y-5, 10, 10);
		g.drawString(""+x, x, y-5);
		g.drawString(""+y, x+5, y+5);
	}
	public void paint(Point screenPos, int zoomDivider, Graphics g){
		g.setColor(Color.magenta);
		int size=0;
		g.fillRect((x-screenPos.x-size/2)/zoomDivider, (y-screenPos.y-size/2)/zoomDivider, size/zoomDivider, size/zoomDivider);
	}
	
/*	public ArrayList<String> getActions() {
		return attackQueue;
	}*/
	public int findNearestEnemy(){
		double shortestRange=0, range;
		int nearestUnit=0;
		double dx, dy;
		for(int c=0; c<main.units.size(); c++){
			if(main.units.get(c)!=null && main.units.get(c).isAlive() && main.units.get(c).getTeam()!=team){
				dx=main.units.get(c).getX()-x;
				dy=main.units.get(c).getY()-y;
				range=(int)Math.sqrt(dx*dx+dy*dy);
				if(shortestRange==0 || shortestRange>=range){
					shortestRange=range;
					nearestUnit=c;
				}
			}
		}
		return nearestUnit;
	}
	public double angleTo(int unitId){
		if(main.units.get(unitId) != null)
			return Math.atan2(main.units.get(unitId).getX()-x, -main.units.get(unitId).getY()+y)/(2*3.1415)-0.25;
		else
			return 0;
	}
	public void onHit(){
		
	}
}